import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import javax.print.attribute.standard.SheetCollate;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

import com.esotericsoftware.kryonet.Server;

public class Class1 extends JPanel implements KeyListener, ActionListener{

	rect rectangle;
	rect rectangle2;
	Map<Integer, rect> badRect = new HashMap<Integer, rect>();

	Map<Integer, circle> circles = new HashMap<Integer, circle>();

	Map<Integer, missile> missiles = new HashMap<Integer, missile>();

	boolean lost = false;
	boolean won = false;
	boolean paused = false;

	static int sWidth;
	static int sHeight;

	static boolean isServer = false;

	String currentMessage;

	Server server;

	Timer t;
	Timer dataT; 

	static Class1 panel;
	static JFrame frame;

	public static void main(String[] args)
	{

		System.out.println("Please enter server for the server program (good rectangle) or client for the client program (bad rectangle) (default is client with server 'localhost'): ");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String option = "";
		try {
			option = br.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}


		if(option.equals("server"))
		{
			panel = new gameServer();
			frame = new JFrame("Best Game Server");
			isServer = true;
		}
		else if(option.equals("client"))
		{
			System.out.println("Please enter an IP to connect to: ");
			String ip = "localhost";
			try {
				ip = br.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.out.println(e.getMessage());
			}
			panel = new gameClient(ip);
			frame = new JFrame("Best Game Client");
		}
		else
		{
			panel = new gameClient("localhost");
			frame = new JFrame("Best Game Client");
		}
		frame.setSize(800, 500);
		frame.getContentPane().setBackground(Color.BLUE);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.addKeyListener(panel);
		frame.add(panel);
		frame.setVisible(true);

		sWidth = frame.getWidth();
		sHeight = frame.getHeight();

		if(!isServer)
		{
			frame.setResizable(false);
		}
	}

	public Class1()
	{
		super();

		rectangle = new rect(10, 1, 30, 30, 10); // x, y, width, height, speed
	}

	@Override
	public void paint(Graphics g)
	{
		Graphics2D g2d = (Graphics2D)g;

		super.paint(g);

		//Paint good rectangle
		g2d.setColor(Color.BLACK);
		g2d.fill(rectangle.rectObject);

		//Paint bad rectangles
		g2d.setColor(Color.BLUE);
		for(rect r: badRect.values())
		{
			g2d.fill(r.rectObject);
		}


		//Paint auto-bad rectangle (AI)
		g2d.fill(rectangle2.rectObject);

		//Paint circles
		g2d.setColor(Color.RED);
		for(circle c: circles.values())
		{
			g2d.fill(c.circObject);
		}

		//Paint missiles
		g2d.setColor(Color.GREEN);
		for(missile m: missiles.values())
		{
			g2d.fill(m.rectObject);
		}

		//Paint message if applicable
		if(currentMessage != null && !currentMessage.equals(""))
		{
			g2d.setColor(Color.ORANGE);
			g2d.drawString("Message: " + currentMessage, 200, panel.getHeight() - 60);
		}

		//Check win/lose cases
		if(lost == true)
		{
			g2d.setColor(Color.BLUE);
			g2d.drawString("You LOSE!  Press ENTER twice to restart (server must restart before clients)!", 500, 300);
		}

		if(won == true)
		{
			g2d.setColor(Color.BLUE);
			g2d.drawString("You WIN!  Press ENTER twice to restart (server must restart before clients)!", 500, 300);
		}

		if(paused == true)
		{
			g2d.setColor(Color.BLUE);
			g2d.drawString("Game paused!  Press O to unpause (P to pause)!", 500, 300);
		}

		g2d.dispose();
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		if (arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
			rectangle.right = true;
			System.out.println("right");
		}
		if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
			rectangle.left = true;
			System.out.println("left");
		}
		if (arg0.getKeyCode() == KeyEvent.VK_UP) {
			rectangle.up = true;
			System.out.println("up");
		}
		if (arg0.getKeyCode() == KeyEvent.VK_DOWN) {
			rectangle.down = true;
			System.out.println("down");
		}
		if(arg0.getKeyCode() == KeyEvent.VK_P)
		{
			t.stop();
			rectangle.paused = true;
		}
		if(arg0.getKeyCode() == KeyEvent.VK_O)
		{
			t.start();
			rectangle.paused = false;
		}
		if(arg0.getKeyCode() == KeyEvent.VK_5)
		{
			for(rect r: badRect.values())
			{
				System.out.println(">>>>Rect ID: " + r.id);
			}
		}
		if(arg0.getKeyCode() == KeyEvent.VK_SPACE)
		{
			if(isServer)
			{
				missile m = new missile(rectangle.rectObject.x, rectangle.rectObject.y, 5, 5, 20);
				m.id = missiles.size();
				missiles.put(m.hashCode(), m);
			}
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		if (arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
			rectangle.right = false;
			System.out.println("right");
		}
		if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
			rectangle.left = false;
			System.out.println("left");
		}
		if (arg0.getKeyCode() == KeyEvent.VK_UP) {
			rectangle.up = false;
			System.out.println("up");
		}
		if (arg0.getKeyCode() == KeyEvent.VK_DOWN) {
			rectangle.down = false;
			System.out.println("down");
		}

		if(arg0.getKeyCode() == KeyEvent.VK_V)
		{
			if(isServer)
			{
				t.stop();
				rectangle.paused = true;

				BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
				System.out.println("PLEASE INPUT THE GAME MESSAGE TO BROADCAST: ");
				try {
					currentMessage = br.readLine();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					System.out.println(e.getMessage());
				}
				t.start();
				rectangle.paused = false;
			}
		}

		if(arg0.getKeyCode() == KeyEvent.VK_ENTER && lost == true)
		{
			lost = false;
			rectangle.x = 10;
			rectangle.y = 50;
			for(rect r: badRect.values())
			{
				r.x = 500;
				r.y = 450;
			}

			rectangle2.x = 500;
			rectangle2.y = 450;

			this.repaint();
			t.start();
		}
		if(arg0.getKeyCode() == KeyEvent.VK_ENTER && won == true)
		{
			won = false;
			rectangle.x = 10;
			rectangle.y = 50;

			for(rect r: badRect.values())
			{
				r.x = 500;
				r.y = 450;
			}

			rectangle2.x = 500;
			rectangle2.y = 450;

			this.repaint();
			t.start();
		}
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub

		if(rectangle.rectObject.intersects(rectangle2.rectObject))
		{
			t.stop();
			lost = true;
			System.out.println("Rectangle intersected rectangle2 *LOSE*");
		}

		if(panel != null)
		{
			rectangle.update(panel.getWidth(), panel.getHeight());
			rectangle2.update(rectangle.rectObject, panel.getWidth(), panel.getHeight());
		}
		this.repaint();
	}

}
